'use strict';
var COS = require('cos-nodejs-sdk-v5');
var fs = require("fs")

const Controller = require('egg').Controller;

class VideoController extends Controller {

  async getVideoRecommend() {

    const {
      ctx
    } = this;

    const data = await ctx.model.Video.findAll({
      'order': [
        ['play_count', 'DESC']
      ],
      include: [{
        model: ctx.model.Info,
        as: 'publisher'
      }],
      limit:8
    })

    ctx.response.body = {
      code: 200,
      msg: 'The query is successful !',
      data
    }


  }

  async getVideo() {

    const {
      ctx
    } = this;

    const {
      id = -1
    } = ctx.query;
    console.log('id = ' + id)

    if (id != -1) {
      const data = await ctx.model.Video.findOne({
        where: {
          id
        },
        include: [{
          model: ctx.model.Info,
          as: 'publisher'
        }],
      })

      ctx.response.body = {
        code: 200,
        msg: 'The query is successful !',
        data
      }
    } else {
      ctx.response.body = {
        code: 404,
        msg: 'not Fond !',
        data: {}
      }
    }

  }

  async testUp() {

    const {
      ctx
    } = this;

    var cos = new COS({
      SecretId: 'AKIDbgW6RHJHVSDjyzrJcHgrrb4GxOffoFBs',
      SecretKey: 'hU1II3KL8e00kFVfaFe3D7l8qKsZPP9f'
    });

    // cos.putObject({
    //   Bucket: 'kong-572-1305771388',
    //   /* 必须 */
    //   Region: 'ap-nanjing',
    //   /* 必须 */
    //   Key: 'test.mp4',
    //   /* 必须 */
    //   Output: fs.createReadStream('./36abbe00da673870eae407b6d13e9eefqt.mp4'),
    // }, function (err, data) {
    //   // ctx.response.body = {
    //   //   code: 200,
    //   //   msg: 'ok!',
    //   //   data: data
    //   // }
    //   console.log(err || data);
    // });

    cos.putObject({
      Bucket: 'kong-572-1305771388',
      /* 必须 */
      Region: 'ap-nanjing',
      /* 必须 */
      Key: 'test.mp4',
      /* 必须 */
      StorageClass: 'STANDARD',
      Body: fs.createReadStream('./36abbe00da673870eae407b6d13e9eefqt.mp4'), // 上传文件对象
      onProgress: function (progressData) {
        console.log(JSON.stringify(progressData));
      }
    }, function (err, data) {
      console.log(err || data);
    });

    // cos.getBucket({
    //   Bucket: 'kong-572-1305771388',
    //   /* 必须 */
    //   Region: 'ap-nanjing',
    //   /* 必须 */
    //   // Prefix: 'a/',
    //   /* 非必须 */
    // }, function (err, data) {
    //   console.log(data.Contents);
    // });

    ctx.response.body = {
      code: 200,
      msg: '!',
      data: {

      }
    }


  }

  async getVideoClass() {
    const {
      ctx
    } = this;

    const {
      limit = 8
    } = ctx.query;

    const list = await ctx.model.VideoClassification.findAll({
      limit,
    })

    ctx.response.body = {
      code: 200,
      msg: 'The query is successful !',
      data: {
        list
      }
    }
  }

  async getVideoList() {
    const {
      ctx
    } = this;

    const {
      tag = 1
    } = ctx.query;

    const list = await ctx.model.Video.findAll({
      where: {
        tag_id: tag
      },
      limit: 8,
      include: [{
        model: ctx.model.Info,
        as: 'publisher'
      }]
    })

    ctx.response.body = {
      code: 200,
      msg: 'The query is successful !',
      data: {
        list
      }
    }
  }
}

module.exports = VideoController;